package com.atguigu.acyjl;

import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;

import com.atguigu.acyjl.activity.MainActivity;

import java.util.ArrayList;

public class MoveUtils {
    TranslateAnimation[] translateAnimations1 = new TranslateAnimation[]{};
    TranslateAnimation[] translateAnimations2 = new TranslateAnimation[]{};
    private ArrayList<TranslateAnimation[]> translateAnimationsList = new ArrayList<>();
    TranslateAnimation ltranslateAnimation;
    TranslateAnimation[] translateAnimations = new TranslateAnimation[8];
    TranslateAnimation w1aTA,w2aTA,w3aTA,w4aTA,w5aTA,w6aTA,w7aTA,w8aTA;
    //一、变量解释
    //translateAnimation1[]  存储locator的平移动画 locator:蓝边中空框,提示用户该在locator所在位置填入词语
    //translateAnimation2[] 存储words的平移动画 words:关卡页面下方8个可点击的按钮选项 作为用户选择的答案
    //translateAnimationList 将translateAnimation1[]，translateAnimation2[]统一起来

    //w1aTA w2aTA……统称为wxaTA 其中↓
    //        w表示word  x表示序号为x的8个可点击按钮之一  T表示平移translate  A表示animation动画
    //translateAnimations 不重要 只是为了给wxaTA赋值用了一次

    //word1Flag word2Flag…… wordxFlag表示某个word被按下  x表示word的序号


    //二、本class内方法汇总
    //          方法总体流程: 传入来自关卡activity的参数→设置具体动画→启动动画
    //1.allSet  //外部变量和本类的接口   外部变量a.allSet()  此方法用来传入外部参数
    //          //以及再将外部参数分类  分别去调用不同的本class内其他方法 如locatorMoveSet、wordMoveSet

    //2.locatorMoveSet //locator提示器具体动画设置
    //3.wordMoveSet //word具体动画设置

    //总上 外部变量调用了allSet 即完成了传入参数+设置具体动画 但未启动

    //4.startMove //外部变量和本类的接口 外部变量b.startMove() 完成启动动画
    //            //通过wordxFlag的值确定某个按钮被点击，从而选择启动对应的动画
    //            //但动画的具体内容 需要事先设置好  在allSet内完成

    public void startMove(TranslateAnimation lTranslateAnimation, TranslateAnimation w1aTA,
                         TranslateAnimation w2aTA, TranslateAnimation w3aTA,
                         TranslateAnimation w4aTA, TranslateAnimation w5aTA,
                         TranslateAnimation w6aTA, TranslateAnimation w7aTA,
                         TranslateAnimation w8aTA, boolean... wordFlag) {
        //这是一个选择启动哪一个动画的方法，但动画的具体内容 需要事先设置好
        translateAnimations1 = new TranslateAnimation[]{lTranslateAnimation};
        translateAnimations2 = new TranslateAnimation[]{w1aTA, w2aTA, w3aTA, w4aTA, w5aTA, w6aTA, w7aTA, w8aTA};
        translateAnimationsList.add(0, translateAnimations1);//list.get(0)[x] 存locator移动
        translateAnimationsList.add(1, translateAnimations2);//list.get(1)[x] 存wordX移动
        if (wordFlag[0]) {//word1被点击，就调用startAnimation
            MainActivity.mainActivity.locator.startAnimation(translateAnimationsList.get(0)[0]);
            MainActivity.mainActivity.word1.startAnimation(translateAnimationsList.get(1)[0]);
        } else if (wordFlag[1]) {//word2被点击，就调用startAnimation
            MainActivity.mainActivity.locator.startAnimation(translateAnimationsList.get(0)[0]);
            MainActivity.mainActivity.word2.startAnimation(translateAnimationsList.get(1)[1]);
        } else if (wordFlag[2]) {
            MainActivity.mainActivity.locator.startAnimation(translateAnimationsList.get(0)[0]);
            MainActivity.mainActivity.word3.startAnimation(translateAnimationsList.get(1)[2]);
        } else if (wordFlag[3]) {
            MainActivity.mainActivity.locator.startAnimation(translateAnimationsList.get(0)[0]);
            MainActivity.mainActivity.word4.startAnimation(translateAnimationsList.get(1)[3]);
        } else if (wordFlag[4]) {
            MainActivity.mainActivity.locator.startAnimation(translateAnimationsList.get(0)[0]);
            MainActivity.mainActivity.word5.startAnimation(translateAnimationsList.get(1)[4]);
        } else if (wordFlag[5]) {
            MainActivity.mainActivity.locator.startAnimation(translateAnimationsList.get(0)[0]);
            MainActivity.mainActivity.word6.startAnimation(translateAnimationsList.get(1)[5]);
        } else if (wordFlag[6]) {
            MainActivity.mainActivity.locator.startAnimation(translateAnimationsList.get(0)[0]);
            MainActivity.mainActivity.word7.startAnimation(translateAnimationsList.get(1)[6]);
        } else if (wordFlag[7]) {
            MainActivity.mainActivity.locator.startAnimation(translateAnimationsList.get(0)[0]);
            MainActivity.mainActivity.word8.startAnimation(translateAnimationsList.get(1)[7]);
        }
    }


    public TranslateAnimation locatortMoveSet(int count,int[] locBlank1,int[] locBlank2,
                                              int[] locBlank3,int[] locBlank4,int[] locBlank5,int[] locBlank6, int[] locBlank7) {
        //locator 就是提示用户 该往locator所在位置填词了  提示框的作用
        TranslateAnimation lTranslateAnimation = new TranslateAnimation(Animation.ABSOLUTE,locBlank1[0]-10,
                Animation.ABSOLUTE,locBlank2[0]-10,
                Animation.ABSOLUTE,locBlank1[1]-90,
                Animation.ABSOLUTE,locBlank2[1]-90);//locator从第一个空白处 移动至 第二个空白处
        if (count == 0) {//当前关卡共点击按钮了多少次 用count+1表示
            //locatorMoving
        }else if(count == 1){//每点击一次按钮 locator就移动至下一个空白处 提示用户填词位置
            lTranslateAnimation = new TranslateAnimation(
                    Animation.ABSOLUTE,locBlank2[0]-10,
                    Animation.ABSOLUTE,locBlank3[0]-10,
                    Animation.ABSOLUTE,locBlank2[1]-90,
                    Animation.ABSOLUTE,locBlank3[1]-90);
        }else if(count==2){
            lTranslateAnimation = new TranslateAnimation(
                    Animation.ABSOLUTE,locBlank3[0]-10,
                    Animation.ABSOLUTE,locBlank4[0]-10,
                    Animation.ABSOLUTE,locBlank3[1]-90,
                    Animation.ABSOLUTE,locBlank4[1]-90);
        }else if(count==3){
            lTranslateAnimation = new TranslateAnimation(
                    Animation.ABSOLUTE,locBlank4[0]-10,
                    Animation.ABSOLUTE,locBlank5[0]-10,
                    Animation.ABSOLUTE,locBlank4[1]-90,
                    Animation.ABSOLUTE,locBlank5[1]-90);
        } else if (count == 4) {
            lTranslateAnimation = new TranslateAnimation(
                    Animation.ABSOLUTE,locBlank5[0]-10,
                    Animation.ABSOLUTE,locBlank6[0]-10,
                    Animation.ABSOLUTE,locBlank5[1]-90,
                    Animation.ABSOLUTE,locBlank6[1]-90);
        } else if (count == 5) {
            lTranslateAnimation = new TranslateAnimation(
                    Animation.ABSOLUTE,locBlank6[0]-10,
                    Animation.ABSOLUTE,locBlank7[0]-10,
                    Animation.ABSOLUTE,locBlank6[1]-90,
                    Animation.ABSOLUTE,locBlank7[1]-90);
        }
        lTranslateAnimation.setDuration(200);
        lTranslateAnimation.setFillAfter(true);
        return lTranslateAnimation;
    }


    public TranslateAnimation[] wordMoveSet(int count, int[] locBlank1, int[] locBlank2,
                                          int[] locBlank3, int[] locBlank4, int[] locBlank5, int[] locBlank6,
                                          Button... word){
        //word就是关卡页面 下方的8个可点击选项（按钮）
        TranslateAnimation[] wxaTA= new TranslateAnimation[8];
        for(int i=0;i<word.length;i++){
            //第一次点击8个按钮之一，按钮从RELATIVE_TO_SELF 0处移动至 locBlank1第一个空白处
            //具体是哪个按钮被点击  用wxaTA[i]表示了
            wxaTA[i] = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,0,
                    Animation.ABSOLUTE,locBlank1[0]-word[i].getLeft(),
                    Animation.RELATIVE_TO_SELF,0,
                    Animation.ABSOLUTE,locBlank1[1]-word[i].getTop()-80);
            wxaTA[i].setDuration(200);
            wxaTA[i].setFillAfter(true);
        }

        if(count==0){//当前关卡共点击按钮了多少次 用count+1表示
            //wordMoving
        }else if(count==1){
            //第二次点击8个按钮之一，按钮从RELATIVE_TO_SELF 0处移动至 locBlank2第二个空白处
            for(int i=0;i<word.length;i++){
                wxaTA[i] = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank2[0]-word[i].getLeft(),
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank2[1]-word[i].getTop()-80);
                wxaTA[i].setDuration(200);
                wxaTA[i].setFillAfter(true);
            }
        }else if(count==2){
            for(int i=0;i<word.length;i++){
                wxaTA[i] = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank3[0]-word[i].getLeft(),
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank3[1]-word[i].getTop()-80);
                wxaTA[i].setDuration(200);
                wxaTA[i].setFillAfter(true);
            }
        }else if(count==3){
            for(int i=0;i<word.length;i++){
                wxaTA[i] = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank4[0]-word[i].getLeft(),
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank4[1]-word[i].getTop()-80);
                wxaTA[i].setDuration(200);
                wxaTA[i].setFillAfter(true);
            }
        }else if(count==4){
            for(int i=0;i<word.length;i++){
                wxaTA[i] = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank5[0]-word[i].getLeft(),
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank5[1]-word[i].getTop()-80);
                wxaTA[i].setDuration(200);
                wxaTA[i].setFillAfter(true);
            }
        }else if(count==5){
            for(int i=0;i<word.length;i++){
                wxaTA[i] = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank6[0]-word[i].getLeft(),
                        Animation.RELATIVE_TO_SELF,0,
                        Animation.ABSOLUTE,locBlank6[1]-word[i].getTop()-80);
                wxaTA[i].setDuration(200);
                wxaTA[i].setFillAfter(true);
            }
        }
        return wxaTA;
    }



    public void allSet(int count, int[] locBlank1, int[] locBlank2, int[] locBlank3,
                       int[] locBlank4, int[] locBlank5, int[] locBlank6, int[] locBlank7){
        //locatorMoving 提示器locator的移动
        ltranslateAnimation = locatortMoveSet(count,locBlank1,locBlank2,
                locBlank3,locBlank4,locBlank5,locBlank6,locBlank7);//传入本class中locatorMoveSet方法需要的参数
        //wordMoving 8个可点击选项的移动
        translateAnimations = wordMoveSet(count,locBlank1,locBlank2,
                locBlank3,locBlank4,locBlank5,locBlank6,MainActivity.mainActivity.word1,
                MainActivity.mainActivity.word2,MainActivity.mainActivity.word3,MainActivity.mainActivity.word4,
                MainActivity.mainActivity.word5,MainActivity.mainActivity.word6,MainActivity.mainActivity.word7,
                MainActivity.mainActivity.word8);//传入本class中WordMoveSet方法需要的参数
        w1aTA = translateAnimations[0];
        w2aTA = translateAnimations[1];
        w3aTA = translateAnimations[2];
        w4aTA = translateAnimations[3];
        w5aTA = translateAnimations[4];
        w6aTA = translateAnimations[5];
        w7aTA = translateAnimations[6];
        w8aTA = translateAnimations[7];
    }
}
